<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>使用jasypt对配置文件加密-基于Springboot进行源码分析 | zylai</title><meta name="keywords" content="安全"><meta name="author" content="zylai"><meta name="copyright" content="zylai"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="0、参考文章  https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844904137423847438 https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;71c2205f0c2a https:&#x2F;&#x2F;www.cnblogs.com&#x2F;wwjj4811&#x2F;p&#x2F;14738704.html  1、配置分析 使用jasypt-spring-boot-starter来进行配置文件密码加密  走个流程，分析一波他的自">
<meta property="og:type" content="article">
<meta property="og:title" content="使用jasypt对配置文件加密-基于Springboot进行源码分析">
<meta property="og:url" content="https://zhaoyunlai.gitee.io/posts/e317d927cf20/index.html">
<meta property="og:site_name" content="zylai">
<meta property="og:description" content="0、参考文章  https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844904137423847438 https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;71c2205f0c2a https:&#x2F;&#x2F;www.cnblogs.com&#x2F;wwjj4811&#x2F;p&#x2F;14738704.html  1、配置分析 使用jasypt-spring-boot-starter来进行配置文件密码加密  走个流程，分析一波他的自">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/jasypt.jpg">
<meta property="article:published_time" content="2022-08-24T11:43:29.000Z">
<meta property="article:modified_time" content="2022-08-25T05:43:20.148Z">
<meta property="article:author" content="zylai">
<meta property="article:tag" content="安全">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/jasypt.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://zhaoyunlai.gitee.io/posts/e317d927cf20/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?ec7714e43e379e663d7258bb72abd0cc";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '使用jasypt对配置文件加密-基于Springboot进行源码分析',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-25 13:43:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">18</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/jasypt.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">zylai</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">使用jasypt对配置文件加密-基于Springboot进行源码分析</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-08-24T11:43:29.000Z" title="发表于 2022-08-24 19:43:29">2022-08-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-25T05:43:20.148Z" title="更新于 2022-08-25 13:43:20">2022-08-25</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%9D%83%E9%99%90%E5%AE%89%E5%85%A8/">权限安全</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="使用jasypt对配置文件加密-基于Springboot进行源码分析"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="0、参考文章">0、参考文章</h2>
<ul>
<li><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844904137423847438">https://juejin.cn/post/6844904137423847438</a></li>
<li><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/71c2205f0c2a">https://www.jianshu.com/p/71c2205f0c2a</a></li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/wwjj4811/p/14738704.html">https://www.cnblogs.com/wwjj4811/p/14738704.html</a></li>
</ul>
<h2 id="1、配置分析">1、配置分析</h2>
<p>使用jasypt-spring-boot-starter来进行配置文件密码加密</p>
<blockquote>
<p>走个流程，分析一波他的自动配置过程</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--用于给配置文件中的密码等加密--&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>看到这个依赖很简洁哇，没有额外的依赖</p>
<p><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/image-20220813175139695.png" alt="image-20220813175139695"></p>
<h3 id="分析自动配置原理">分析自动配置原理</h3>
<p>流程就是从自动配置类<code>xxxAutoConfiguration</code>–&gt;自动配置类里面注册或者导入了组件 --&gt;<code>xxxProperties</code>里面有属性值–&gt;绑定了配置文件中以<code>xxx.xxxx</code>开头的配置项</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//自动配置类</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Import(EnableEncryptablePropertiesConfiguration.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JasyptSpringBootAutoConfiguration</span> &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//导入的组件的 EnableEncryptablePropertiesConfiguration</span></span><br><span class="line"><span class="comment">//这里还是没有出现配置绑定，继续找，从导入的这两个类中看，先看EncryptablePropertyResolverConfiguration</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Import(&#123;EncryptablePropertyResolverConfiguration.class, CachingConfiguration.class&#125;)</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EnableEncryptablePropertiesConfiguration</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//里面只注册了这一个组件</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> EnableEncryptablePropertiesBeanFactoryPostProcessor <span class="title function_">enableEncryptablePropertySourcesPostProcessor</span><span class="params">(<span class="keyword">final</span> ConfigurableEnvironment environment, EncryptablePropertySourceConverter converter)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">EnableEncryptablePropertiesBeanFactoryPostProcessor</span>(environment, converter);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>EncryptablePropertyResolverConfiguration</code>，这个类里面注册了很多组件，其中有一个组件为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EncryptablePropertyResolverConfiguration</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//其中有一个bean为，看一下这个单例的泛型JasyptEncryptorConfigurationProperties</span></span><br><span class="line">    <span class="comment">//这个名字比较明显了，就是配置类，所以点进去</span></span><br><span class="line">    <span class="meta">@Bean(name = CONFIG_SINGLETON)</span></span><br><span class="line">    <span class="keyword">public</span> Singleton&lt;JasyptEncryptorConfigurationProperties&gt; <span class="title function_">configProps</span><span class="params">(</span></span><br><span class="line"><span class="params">            <span class="keyword">final</span> EnvCopy envCopy)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt; JasyptEncryptorConfigurationProperties.bindConfigProps(envCopy.get()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>JasyptEncryptorConfigurationProperties</code>：激动人心吧，终于找到了配置绑定类。可以看到绑定了配置文件<code>jasypt.encryptor</code>开头的属性，所以直接在<code>application.yaml</code>文件中该属性就可以了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ConfigurationProperties(prefix = &quot;jasypt.encryptor&quot;, ignoreUnknownFields = true)</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JasyptEncryptorConfigurationProperties</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//这里简单列一下几个属性</span></span><br><span class="line">    </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Specify the name of bean to override jasypt-spring-boot&#x27;s default properties based</span></span><br><span class="line"><span class="comment">     * &#123;<span class="doctag">@link</span> org.jasypt.encryption.StringEncryptor&#125;. Default Value is &#123;<span class="doctag">@code</span> jasyptStringEncryptor&#125;.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//指定加密器对应的bean名称</span></span><br><span class="line">    <span class="keyword">private</span> <span class="type">String</span> <span class="variable">bean</span> <span class="operator">=</span> <span class="string">&quot;jasyptStringEncryptor&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//里面默认的算法为：</span></span><br><span class="line">    <span class="keyword">private</span> <span class="type">String</span> <span class="variable">algorithm</span> <span class="operator">=</span> <span class="string">&quot;PBEWITHHMACSHA512ANDAES_256&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//密匙没有默认值，需要我们在配置文件中指定</span></span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//字符串输出的编码格式</span></span><br><span class="line">	<span class="keyword">private</span> <span class="type">String</span> <span class="variable">stringOutputType</span> <span class="operator">=</span> <span class="string">&quot;base64&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//嵌套的配置属性，在jasypt.encryptor基础上再加上.property</span></span><br><span class="line">    <span class="meta">@NestedConfigurationProperty</span></span><br><span class="line">    <span class="keyword">private</span> <span class="type">PropertyConfigurationProperties</span> <span class="variable">property</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PropertyConfigurationProperties</span>();</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Data</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">PropertyConfigurationProperties</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//前缀</span></span><br><span class="line">        <span class="keyword">private</span> <span class="type">String</span> <span class="variable">prefix</span> <span class="operator">=</span> <span class="string">&quot;ENC(&quot;</span>;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//后缀</span></span><br><span class="line">        <span class="keyword">private</span> <span class="type">String</span> <span class="variable">suffix</span> <span class="operator">=</span> <span class="string">&quot;)&quot;</span>;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="那么我们看一下生成密匙的工具类">那么我们看一下生成密匙的工具类</h3>
<p>在<code>EncryptablePropertyResolverConfiguration</code>中进行的注册，所以我们使用的时候直接<code>@Autowired</code>。</p>
<p>返回的加密器是<code>DefaultLazyEncryptor</code>类型，简单看一下他的构造器参数：</p>
<ul>
<li><code>envCopy.get()</code>获取了配置文件中的参数</li>
<li><code>customEncryptorBeanName</code>用户自定义加密器的bean名称</li>
<li><code>isCustom</code>用户是否自定义了加密器，jasypt.encryptor.bean默认有值为<code>jasyptStringEncryptor</code>，也可以在配置文件中指定</li>
<li><code>bf</code>Bean工厂</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EncryptablePropertyResolverConfiguration</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_PROPERTY</span> <span class="operator">=</span> <span class="string">&quot;jasypt.encryptor.bean&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_PLACEHOLDER</span> <span class="operator">=</span> String.format(<span class="string">&quot;$&#123;%s:jasyptStringEncryptor&#125;&quot;</span>, ENCRYPTOR_BEAN_PROPERTY);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_NAME</span> <span class="operator">=</span> <span class="string">&quot;lazyJasyptStringEncryptor&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean(name = ENCRYPTOR_BEAN_NAME)</span></span><br><span class="line">    <span class="keyword">public</span> StringEncryptor <span class="title function_">stringEncryptor</span><span class="params">(</span></span><br><span class="line"><span class="params">            <span class="keyword">final</span> EnvCopy envCopy,</span></span><br><span class="line"><span class="params">            <span class="keyword">final</span> BeanFactory bf)</span> &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">String</span> <span class="variable">customEncryptorBeanName</span> <span class="operator">=</span> envCopy.get().resolveRequiredPlaceholders(ENCRYPTOR_BEAN_PLACEHOLDER);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isCustom</span> <span class="operator">=</span> envCopy.get().containsProperty(ENCRYPTOR_BEAN_PROPERTY);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">DefaultLazyEncryptor</span>(envCopy.get(), customEncryptorBeanName, isCustom, bf);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="2、开始使用">2、开始使用</h2>
<p>使用的大致流程是，我们根据加密规则生成明文的密码，然后用这些密码代替原来的明文。那么系统启动的时候会根据我们的配置进行解密。</p>
<h3 id="2-1-配置文件">2.1 配置文件</h3>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">jasypt:</span></span><br><span class="line">  <span class="attr">encryptor:</span></span><br><span class="line"><span class="comment">#    加密密匙</span></span><br><span class="line">    <span class="attr">password:</span> <span class="string">zylai</span></span><br></pre></td></tr></table></figure>
<p>在这个配置文件中指定密匙</p>
<h3 id="2-2-加密明文">2.2 加密明文</h3>
<p>我们直接在测试类中获取配置文件中的明文，然后加密</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EncryptTest</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//加密的工具</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StringEncryptor stringEncryptor;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ApplicationContext applicationContext;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">getEncryptedPwd</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="type">Environment</span> <span class="variable">environment</span> <span class="operator">=</span> applicationContext.getBean(Environment.class);</span><br><span class="line">        <span class="comment">//获取配置文件中的明文密码</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">mysqlPassword</span> <span class="operator">=</span> environment.getProperty(<span class="string">&quot;spring.datasource.password&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">redisPassword</span> <span class="operator">=</span> environment.getProperty(<span class="string">&quot;spring.redis.password&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;明文信息为如下：&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;mysqlPassword:&quot;</span>+mysqlPassword+<span class="string">&quot;\nredisPassword:&quot;</span>+redisPassword);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;========加密之后的信息如下===============&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;mysqlPassword:&quot;</span>+encrypt(mysqlPassword));</span><br><span class="line">        System.out.println(<span class="string">&quot;redisPassword:&quot;</span>+encrypt(redisPassword));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">encrypt</span><span class="params">(String str)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> stringEncryptor.encrypt(str);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/image-20220813220059539.png" alt="image-20220813220059539"></p>
<h3 id="2-3-修改配置文件">2.3 修改配置文件</h3>
<p>将上面的加密字符串代替配置文件中的明文即可，注意需要将加密的结果放到<code>ENC()</code>里面，为什么是ENC呢？这个是默认的设置，程序启动时会去解析这个括号里面的密码。</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring.redis.password</span>=<span class="string">ENC(T5os7CbvjZ3N33Ns96ikjfSIFByMRhxhBNb+G6qWLhbn3vpZ0miP+HKfRQDiyhts)</span></span><br><span class="line"><span class="attr">spring.datasource.password</span>=<span class="string">ENC(6eAFfiW1eGeYH+enb1A0rmcsI+Jw5hFZe5Earb2Fu3SrwaDQaXbZ8DASDTbjI5R7)</span></span><br></pre></td></tr></table></figure>
<p>其实我们可以自定义<code>ENC()</code>，只需要添加属性即可</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">jasypt:</span></span><br><span class="line">  <span class="attr">encryptor:</span></span><br><span class="line"><span class="comment">#    加密密匙</span></span><br><span class="line">    <span class="attr">password:</span> <span class="string">zylai</span></span><br><span class="line">    <span class="attr">property:</span></span><br><span class="line">      <span class="attr">prefix:</span> <span class="string">ZYLai(</span></span><br><span class="line">      <span class="attr">suffix:</span> <span class="string">)</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>在第一部分自动配置分析中，从配置绑定类的属性就可以看出我们可以自定义</p>
</blockquote>
<p>接下来直接启动程序验证一下即可，然后在主启动类中，我们再去获取一下这些密码看看程序是否能够自动解密（只要程序启动成功一般就是成功了）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Boot04WebAdminApplication</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">ConfigurableApplicationContext</span> <span class="variable">run</span> <span class="operator">=</span> SpringApplication.run(Boot04WebAdminApplication.class, args);</span><br><span class="line">        <span class="type">Environment</span> <span class="variable">environment</span> <span class="operator">=</span> run.getBean(Environment.class);</span><br><span class="line">        <span class="comment">//查看一下加密的密匙，测试用</span></span><br><span class="line">        System.out.println(environment.getProperty(<span class="string">&quot;jasypt.encryptor.password&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//在程序启动时，jasypt会解密ENC()中的内容</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">mysqlPassword</span> <span class="operator">=</span> environment.getProperty(<span class="string">&quot;spring.datasource.password&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">mysqlUsername</span> <span class="operator">=</span> environment.getProperty(<span class="string">&quot;spring.datasource.username&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;测试数据加密，解密之后的mysql账号密码为：&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;mysqlPassword:&quot;</span>+mysqlPassword+<span class="string">&quot;\nmysqlUsername:&quot;</span>+mysqlUsername);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>大功告成！</p>
<p><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/image-20220813221014559.png" alt="image-20220813221014559"></p>
<h3 id="2-4-启动程序的日志">2.4 启动程序的日志</h3>
<p>同时在启动的日志中可以看到，如果没有进行配置值，就会使用默认的</p>
<p><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/image-20220813222041864.png" alt="image-20220813222041864"></p>
<p>这个日志是在加密器<code>DefaultLazyEncryptor</code>中执行的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DefaultLazyEncryptor</span> <span class="keyword">implements</span> <span class="title class_">StringEncryptor</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Singleton&lt;StringEncryptor&gt; singleton;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyEncryptor</span><span class="params">(<span class="keyword">final</span> ConfigurableEnvironment e, <span class="keyword">final</span> String customEncryptorBeanName, <span class="type">boolean</span> isCustom, <span class="keyword">final</span> BeanFactory bf)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt;</span><br><span class="line">                Optional.of(customEncryptorBeanName)</span><br><span class="line">                        .filter(bf::containsBean)</span><br><span class="line">                        .map(name -&gt; (StringEncryptor) bf.getBean(name))</span><br><span class="line">                        .map(tap(bean -&gt; log.info(<span class="string">&quot;Found Custom Encryptor Bean &#123;&#125; with name: &#123;&#125;&quot;</span>, bean, customEncryptorBeanName)))</span><br><span class="line">                        .orElseGet(() -&gt; &#123;</span><br><span class="line">                            <span class="keyword">if</span> (isCustom) &#123;</span><br><span class="line">                                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(String.format(<span class="string">&quot;String Encryptor custom Bean not found with name &#x27;%s&#x27;&quot;</span>, customEncryptorBeanName));</span><br><span class="line">                            &#125;</span><br><span class="line">                            log.info(<span class="string">&quot;String Encryptor custom Bean not found with name &#x27;&#123;&#125;&#x27;. Initializing Default String Encryptor&quot;</span>, customEncryptorBeanName);</span><br><span class="line">                            <span class="keyword">return</span> createDefault(e);</span><br><span class="line">                        &#125;));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyEncryptor</span><span class="params">(<span class="keyword">final</span> ConfigurableEnvironment e)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt; createDefault(e));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> StringEncryptor <span class="title function_">createDefault</span><span class="params">(ConfigurableEnvironment e)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">StringEncryptorBuilder</span>(JasyptEncryptorConfigurationProperties.bindConfigProps(e), <span class="string">&quot;jasypt.encryptor&quot;</span>).build();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">encrypt</span><span class="params">(<span class="keyword">final</span> String message)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> singleton.get().encrypt(message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">decrypt</span><span class="params">(<span class="keyword">final</span> String encryptedMessage)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> singleton.get().decrypt(encryptedMessage);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>那么如何自定义这个属性呢？在第四部分详细展开</p>
<h2 id="3、配置文件加密了？但又没完全加密">3、配置文件加密了？但又没完全加密</h2>
<p>虽然在配置文件中没有密码等敏感信息了，但是他又加密的密匙<code>jasypt.encryptor.password</code>哇，别人仍然可以拿着这个密匙进行解密来获取你的敏感信息。所以密匙可以不写在配置文件中！！！</p>
<p>三种方式</p>
<p>方式一：直接作为程序启动时的<strong>命令行参数</strong>来带入</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar yourproject.jar --jasypt.encryptor.password=zylai</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>方式二</strong>：直接作为程序启动时的<strong>应用环境变量</strong>来带入</li>
</ul>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">java <span class="attr">-Djasypt.encryptor.password</span>=zyali -jar yourproject.jar</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>
<blockquote>
<p>对于已经完成的项目进行加密可以使用以上两种方式，但对于个人正在开发的项目，可以试试使用系统的环境变量。</p>
</blockquote>
<ul>
<li><strong>方式三</strong>：甚至可以作为<strong>系统环境变量</strong>的方式来带入</li>
</ul>
<p>我们提前设置好系统环境变量<code>JASYPT_ENCRYPTOR_PASSWORD = zylai</code>，则直接在Spring Boot的项目配置文件中做如下配置即可：</p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">jasypt.encryptor.password</span>=<span class="variable">$&#123;JASYPT_ENCRYPTOR_PASSWORD&#125;</span></span><br></pre></td></tr></table></figure>
<h2 id="4、自定义功能-分析源码">4、自定义功能-分析源码</h2>
<h3 id="加密器分析">加密器分析</h3>
<p>在配置类<code>JasyptEncryptorConfigurationProperties</code>中可以看到指定了加密器对应的bean名称</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * Specify the name of bean to override jasypt-spring-boot&#x27;s default properties based</span></span><br><span class="line"><span class="comment">  * &#123;<span class="doctag">@link</span> org.jasypt.encryption.StringEncryptor&#125;. Default Value is &#123;<span class="doctag">@code</span> jasyptStringEncryptor&#125;.</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> <span class="comment">//指定加密器对应的bean名称</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">String</span> <span class="variable">bean</span> <span class="operator">=</span> <span class="string">&quot;jasyptStringEncryptor&quot;</span>;</span><br></pre></td></tr></table></figure>
<p>官方的注释说指定bean名称来重写默认的加密器，所以我们自己配置的加密器的名称最好也是这个名字，或者在配置文件中显式指定这个名字</p>
<p>那么这个bean是在哪里注册的呢？我们再回到<code>EncryptablePropertyResolverConfiguration</code>，这个类里面注册了很多组件</p>
<p>值得注意的是，这个<code>StringEncryptor</code>接口bean的名称为<code>lazyJasyptStringEncryptor</code>，并不是<code>jasyptStringEncryptor</code>，但是他的返回值（即实现类）是一个<code>DefaultLazyEncryptor</code>，其构造器中指定了这个bean的名称</p>
<p>分析一下这个方法的返回值内容<code>new DefaultLazyEncryptor(envCopy.get(), customEncryptorBeanName, isCustom, bf);</code></p>
<ul>
<li><code>customEncryptorBeanName</code>指定了用户定义加密器的bean名称，名称为<code>String.format(&quot;$&#123;%s:jasyptStringEncryptor&#125;&quot;, ENCRYPTOR_BEAN_PROPERTY)</code>，就是从ENCRYPTOR_BEAN_PROPERTY中获取，如果没有值就使用<code>jasyptStringEncryptor</code>。</li>
<li><code>isCustom</code>：用户是否自定义了，判断方式为：<code>envCopy.get().containsProperty(ENCRYPTOR_BEAN_PROPERTY);</code>，在配置类中有默认值，我们也可以在配置文件中指定</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EncryptablePropertyResolverConfiguration</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_PROPERTY</span> <span class="operator">=</span> <span class="string">&quot;jasypt.encryptor.bean&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_PLACEHOLDER</span> <span class="operator">=</span> String.format(<span class="string">&quot;$&#123;%s:jasyptStringEncryptor&#125;&quot;</span>, ENCRYPTOR_BEAN_PROPERTY);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ENCRYPTOR_BEAN_NAME</span> <span class="operator">=</span> <span class="string">&quot;lazyJasyptStringEncryptor&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean(name = ENCRYPTOR_BEAN_NAME)</span></span><br><span class="line">    <span class="keyword">public</span> StringEncryptor <span class="title function_">stringEncryptor</span><span class="params">(</span></span><br><span class="line"><span class="params">            <span class="keyword">final</span> EnvCopy envCopy,</span></span><br><span class="line"><span class="params">            <span class="keyword">final</span> BeanFactory bf)</span> &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">String</span> <span class="variable">customEncryptorBeanName</span> <span class="operator">=</span> envCopy.get().resolveRequiredPlaceholders(ENCRYPTOR_BEAN_PLACEHOLDER);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isCustom</span> <span class="operator">=</span> envCopy.get().containsProperty(ENCRYPTOR_BEAN_PROPERTY);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">DefaultLazyEncryptor</span>(envCopy.get(), customEncryptorBeanName, isCustom, bf);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>我们再看一下<code>DefaultLazyEncryptor</code>构造器的执行为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DefaultLazyEncryptor</span> <span class="keyword">implements</span> <span class="title class_">StringEncryptor</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Singleton&lt;StringEncryptor&gt; singleton;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyEncryptor</span><span class="params">(<span class="keyword">final</span> ConfigurableEnvironment e, <span class="keyword">final</span> String customEncryptorBeanName, <span class="type">boolean</span> isCustom, <span class="keyword">final</span> BeanFactory bf)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt;</span><br><span class="line">                Optional.of(customEncryptorBeanName)</span><br><span class="line">                        .filter(bf::containsBean)</span><br><span class="line">                        .map(name -&gt; (StringEncryptor) bf.getBean(name))</span><br><span class="line">                        .map(tap(bean -&gt; log.info(<span class="string">&quot;Found Custom Encryptor Bean &#123;&#125; with name: &#123;&#125;&quot;</span>, bean, customEncryptorBeanName)))</span><br><span class="line">                        .orElseGet(() -&gt; &#123;</span><br><span class="line">                            <span class="keyword">if</span> (isCustom) &#123;</span><br><span class="line">                                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(String.format(<span class="string">&quot;String Encryptor custom Bean not found with name &#x27;%s&#x27;&quot;</span>, customEncryptorBeanName));</span><br><span class="line">                            &#125;</span><br><span class="line">                            log.info(<span class="string">&quot;String Encryptor custom Bean not found with name &#x27;&#123;&#125;&#x27;. Initializing Default String Encryptor&quot;</span>, customEncryptorBeanName);</span><br><span class="line">                            <span class="keyword">return</span> createDefault(e);</span><br><span class="line">                        &#125;));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyEncryptor</span><span class="params">(<span class="keyword">final</span> ConfigurableEnvironment e)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt; createDefault(e));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> StringEncryptor <span class="title function_">createDefault</span><span class="params">(ConfigurableEnvironment e)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">StringEncryptorBuilder</span>(JasyptEncryptorConfigurationProperties.bindConfigProps(e), <span class="string">&quot;jasypt.encryptor&quot;</span>).build();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">encrypt</span><span class="params">(<span class="keyword">final</span> String message)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> singleton.get().encrypt(message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">decrypt</span><span class="params">(<span class="keyword">final</span> String encryptedMessage)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> singleton.get().decrypt(encryptedMessage);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>一套分析下来应该也明白了，就是我们可以自定义加密器（bean的返回值类型要是<code>StringEncryptor</code>），加密器bean的默认名称为jasyptStringEncryptor，如果我们自定了名称，需要在配置文件中指定<code>jasypt.encryptor.bean</code></p>
<h4 id="自定义的加密器">自定义的加密器</h4>
<blockquote>
<p>密匙一定要从配置文件中获取，要是自己再写明文，那不是白忙活了吗</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EncryptorConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean(&quot;jasyptStringEncryptor&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> StringEncryptor <span class="title function_">zylaiEncryptor</span><span class="params">(<span class="meta">@Value(&quot;$&#123;jasypt.encryptor.password&#125;&quot;)</span> String password)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">PooledPBEStringEncryptor</span> <span class="variable">encryptor</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PooledPBEStringEncryptor</span>();</span><br><span class="line">        <span class="type">SimpleStringPBEConfig</span> <span class="variable">config</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleStringPBEConfig</span>();</span><br><span class="line">        config.setPassword(password);</span><br><span class="line">        config.setAlgorithm(<span class="string">&quot;PBEWITHHMACSHA512ANDAES_256&quot;</span>);</span><br><span class="line">        config.setKeyObtentionIterations(<span class="string">&quot;1000&quot;</span>);</span><br><span class="line">        config.setPoolSize(<span class="string">&quot;1&quot;</span>);</span><br><span class="line"><span class="comment">//        config.setProviderName(&quot;zylai&quot;);</span></span><br><span class="line">        config.setSaltGeneratorClassName(<span class="string">&quot;org.jasypt.salt.RandomSaltGenerator&quot;</span>);</span><br><span class="line">        config.setIvGeneratorClassName(<span class="string">&quot;org.jasypt.iv.RandomIvGenerator&quot;</span>);</span><br><span class="line">        config.setStringOutputType(<span class="string">&quot;base64&quot;</span>);</span><br><span class="line">        encryptor.setConfig(config);</span><br><span class="line">        <span class="keyword">return</span> encryptor;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5、关于解密">5、关于解密</h2>
<h3 id="是否需要自定义解密器">是否需要自定义解密器</h3>
<p>是否有个问题，我们自定义了加密器，那么解密器是不是也要自己定义一个，来对应加密器？答案是完全不用哈</p>
<p>首先，加密器的接口<code>StringEncryptor</code>，长这个样子。看见没，不仅仅只是加密，也有解密的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">StringEncryptor</span> &#123;</span><br><span class="line">    String <span class="title function_">encrypt</span><span class="params">(String var1)</span>;</span><br><span class="line"></span><br><span class="line">    String <span class="title function_">decrypt</span><span class="params">(String var1)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>再看一下如何向容器中注册的解密器：</p>
<p>这个方法和注册加密器的方法几乎一样，支持用户的自定义，返回<code>DefaultLazyPropertyResolver</code>实例，但是他的构造器多了一个参数为<code>encryptor</code>，这个参数就是从容器中获取的<code>@Qualifier(ENCRYPTOR_BEAN_NAME) final StringEncryptor encryptor</code>，哈哈经典的ENCRYPTOR_BEAN_NAME，又回来了吧，所以说加密和解密都是基于容器中名为ENCRYPTOR_BEAN_NAME，类型为<code>StringEncryptor </code>的组件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean(name = RESOLVER_BEAN_NAME)</span></span><br><span class="line"><span class="keyword">public</span> EncryptablePropertyResolver <span class="title function_">encryptablePropertyResolver</span><span class="params">(</span></span><br><span class="line"><span class="params">        <span class="meta">@Qualifier(DETECTOR_BEAN_NAME)</span> <span class="keyword">final</span> EncryptablePropertyDetector propertyDetector,</span></span><br><span class="line"><span class="params">        <span class="meta">@Qualifier(ENCRYPTOR_BEAN_NAME)</span> <span class="keyword">final</span> StringEncryptor encryptor, <span class="keyword">final</span> BeanFactory bf,</span></span><br><span class="line"><span class="params">        <span class="keyword">final</span> EnvCopy envCopy, <span class="keyword">final</span> ConfigurableEnvironment environment)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">String</span> <span class="variable">customResolverBeanName</span> <span class="operator">=</span> envCopy.get().resolveRequiredPlaceholders(RESOLVER_BEAN_PLACEHOLDER);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">isCustom</span> <span class="operator">=</span> envCopy.get().containsProperty(RESOLVER_BEAN_PROPERTY);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">DefaultLazyPropertyResolver</span>(propertyDetector, encryptor, customResolverBeanName, isCustom, bf, environment);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="解密的方法">解密的方法</h3>
<p>上面看到容器中会注册一个<code>DefaultLazyPropertyResolver</code>组件，构造器中单例最终创建的是<code>DefaultPropertyResolver</code>类型</p>
<blockquote>
<p>EncryptablePropertyResolver 接口 就俩实现类，除了这一个就是DefaultPropertyResolver，在DefaultPropertyResolver中真正实现了解密</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DefaultLazyPropertyResolver</span> <span class="keyword">implements</span> <span class="title class_">EncryptablePropertyResolver</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Singleton&lt;EncryptablePropertyResolver&gt; singleton;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyPropertyResolver</span><span class="params">(EncryptablePropertyDetector propertyDetector, StringEncryptor encryptor, String customResolverBeanName, <span class="type">boolean</span> isCustom, BeanFactory bf, Environment environment)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt;</span><br><span class="line">                Optional.of(customResolverBeanName)</span><br><span class="line">                        .filter(bf::containsBean)</span><br><span class="line">                        .map(name -&gt; (EncryptablePropertyResolver) bf.getBean(name))</span><br><span class="line">                        .map(tap(bean -&gt; log.info(<span class="string">&quot;Found Custom Resolver Bean &#123;&#125; with name: &#123;&#125;&quot;</span>, bean, customResolverBeanName)))</span><br><span class="line">                        .orElseGet(() -&gt; &#123;</span><br><span class="line">                            <span class="keyword">if</span> (isCustom) &#123;</span><br><span class="line">                                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(String.format(<span class="string">&quot;Property Resolver custom Bean not found with name &#x27;%s&#x27;&quot;</span>, customResolverBeanName));</span><br><span class="line">                            &#125;</span><br><span class="line">                            log.info(<span class="string">&quot;Property Resolver custom Bean not found with name &#x27;&#123;&#125;&#x27;. Initializing Default Property Resolver&quot;</span>, customResolverBeanName);</span><br><span class="line">                            <span class="keyword">return</span> createDefault(propertyDetector, encryptor, environment);</span><br><span class="line">                        &#125;));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultLazyPropertyResolver</span><span class="params">(EncryptablePropertyDetector propertyDetector, StringEncryptor encryptor, Environment environment)</span> &#123;</span><br><span class="line">        singleton = <span class="keyword">new</span> <span class="title class_">Singleton</span>&lt;&gt;(() -&gt; createDefault(propertyDetector, encryptor, environment));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建DefaultPropertyResolver对象</span></span><br><span class="line">    <span class="keyword">private</span> DefaultPropertyResolver <span class="title function_">createDefault</span><span class="params">(EncryptablePropertyDetector propertyDetector, StringEncryptor encryptor, Environment environment)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">DefaultPropertyResolver</span>(encryptor, propertyDetector, environment);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">resolvePropertyValue</span><span class="params">(String value)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> singleton.get().resolvePropertyValue(value);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>DefaultPropertyResolver类中主要看<code>resolvePropertyValue</code>他会根据<code>StringEncryptor</code>实例中的<code>decrypt</code>方法进行解密，<strong>并将解密之后的结果再放回配置环境中</strong>，也就有了之前我们看到程序启动之后，在启动类中直接获取配置文件中加密的属性的明文。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DefaultPropertyResolver</span> <span class="keyword">implements</span> <span class="title class_">EncryptablePropertyResolver</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Environment environment;</span><br><span class="line">    <span class="keyword">private</span> StringEncryptor encryptor;</span><br><span class="line">    <span class="keyword">private</span> EncryptablePropertyDetector detector;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultPropertyResolver</span><span class="params">(StringEncryptor encryptor, Environment environment)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>(encryptor, <span class="keyword">new</span> <span class="title class_">DefaultPropertyDetector</span>(), environment);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">DefaultPropertyResolver</span><span class="params">(StringEncryptor encryptor, EncryptablePropertyDetector detector, Environment environment)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.environment = environment;</span><br><span class="line">        Assert.notNull(encryptor, <span class="string">&quot;String encryptor can&#x27;t be null&quot;</span>);</span><br><span class="line">        Assert.notNull(detector, <span class="string">&quot;Encryptable Property detector can&#x27;t be null&quot;</span>);</span><br><span class="line">        <span class="built_in">this</span>.encryptor = encryptor;</span><br><span class="line">        <span class="built_in">this</span>.detector = detector;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">resolvePropertyValue</span><span class="params">(String value)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> Optional.ofNullable(value)</span><br><span class="line">                .map(environment::resolvePlaceholders)</span><br><span class="line">                .filter(detector::isEncrypted)</span><br><span class="line">                .map(resolvedValue -&gt; &#123;</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="type">String</span> <span class="variable">unwrappedProperty</span> <span class="operator">=</span> detector.unwrapEncryptedValue(resolvedValue.trim());</span><br><span class="line">                        <span class="type">String</span> <span class="variable">resolvedProperty</span> <span class="operator">=</span> environment.resolvePlaceholders(unwrappedProperty);</span><br><span class="line">                        <span class="keyword">return</span> encryptor.decrypt(resolvedProperty);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (EncryptionOperationNotPossibleException e) &#123;</span><br><span class="line">                        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">DecryptionException</span>(<span class="string">&quot;Unable to decrypt: &quot;</span> + value + <span class="string">&quot;. Decryption of Properties failed,  make sure encryption/decryption &quot;</span> +</span><br><span class="line">                                <span class="string">&quot;passwords match&quot;</span>, e);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">                .orElse(value);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="总结-v2">总结</h3>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://zhaoyunlai.gitee.io">zylai</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhaoyunlai.gitee.io/posts/e317d927cf20/">https://zhaoyunlai.gitee.io/posts/e317d927cf20/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhaoyunlai.gitee.io" target="_blank">zylai</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%AE%89%E5%85%A8/">安全</a></div><div class="post_share"><div class="social-share" data-image="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/jasypt.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/e32230f09e09/"><img class="prev-cover" src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">学生成长动态追踪系统</div></div></a></div><div class="next-post pull-right"><a href="/posts/32afa3256671/"><img class="next-cover" src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/SpringBoot-01.assets/springboot01.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">SpringBoot2自动装配原理以及入门使用</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/78afc3f0ab42/" title="Shiro权限安全框架的使用"><img class="cover" src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/shiro.assets/image-20220929162947122.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-18</div><div class="title">Shiro权限安全框架的使用</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">zylai</div><div class="author-info__description">zylai && 管大虾の克星</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">18</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/zhaoyunlai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/zhaoyunlai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:zylai0712@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#0%E3%80%81%E5%8F%82%E8%80%83%E6%96%87%E7%AB%A0"><span class="toc-text">0、参考文章</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E9%85%8D%E7%BD%AE%E5%88%86%E6%9E%90"><span class="toc-text">1、配置分析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%9E%90%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE%E5%8E%9F%E7%90%86"><span class="toc-text">分析自动配置原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%82%A3%E4%B9%88%E6%88%91%E4%BB%AC%E7%9C%8B%E4%B8%80%E4%B8%8B%E7%94%9F%E6%88%90%E5%AF%86%E5%8C%99%E7%9A%84%E5%B7%A5%E5%85%B7%E7%B1%BB"><span class="toc-text">那么我们看一下生成密匙的工具类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8"><span class="toc-text">2、开始使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-text">2.1 配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E5%8A%A0%E5%AF%86%E6%98%8E%E6%96%87"><span class="toc-text">2.2 加密明文</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E4%BF%AE%E6%94%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-text">2.3 修改配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-%E5%90%AF%E5%8A%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%97%A5%E5%BF%97"><span class="toc-text">2.4 启动程序的日志</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%8A%A0%E5%AF%86%E4%BA%86%EF%BC%9F%E4%BD%86%E5%8F%88%E6%B2%A1%E5%AE%8C%E5%85%A8%E5%8A%A0%E5%AF%86"><span class="toc-text">3、配置文件加密了？但又没完全加密</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8A%9F%E8%83%BD-%E5%88%86%E6%9E%90%E6%BA%90%E7%A0%81"><span class="toc-text">4、自定义功能-分析源码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%A0%E5%AF%86%E5%99%A8%E5%88%86%E6%9E%90"><span class="toc-text">加密器分析</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%9A%84%E5%8A%A0%E5%AF%86%E5%99%A8"><span class="toc-text">自定义的加密器</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E5%85%B3%E4%BA%8E%E8%A7%A3%E5%AF%86"><span class="toc-text">5、关于解密</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%98%AF%E5%90%A6%E9%9C%80%E8%A6%81%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%A3%E5%AF%86%E5%99%A8"><span class="toc-text">是否需要自定义解密器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E5%AF%86%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">解密的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93-v2"><span class="toc-text">总结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/22ef9dff46af/" title="移动互联网开发技术-第三次实验"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="移动互联网开发技术-第三次实验"/></a><div class="content"><a class="title" href="/posts/22ef9dff46af/" title="移动互联网开发技术-第三次实验">移动互联网开发技术-第三次实验</a><time datetime="2022-10-15T01:43:30.000Z" title="发表于 2022-10-15 09:43:30">2022-10-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/85838fb07a8b/" title="学生成长动态追踪系统-学生使用手册"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统-学生使用手册"/></a><div class="content"><a class="title" href="/posts/85838fb07a8b/" title="学生成长动态追踪系统-学生使用手册">学生成长动态追踪系统-学生使用手册</a><time datetime="2022-09-25T11:43:30.000Z" title="发表于 2022-09-25 19:43:30">2022-09-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/896511b44bb7/" title="学生成长动态追踪系统-家长使用手册"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统-家长使用手册"/></a><div class="content"><a class="title" href="/posts/896511b44bb7/" title="学生成长动态追踪系统-家长使用手册">学生成长动态追踪系统-家长使用手册</a><time datetime="2022-09-25T11:43:29.000Z" title="发表于 2022-09-25 19:43:29">2022-09-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/78afc3f0ab42/" title="Shiro权限安全框架的使用"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/shiro.assets/image-20220929162947122.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Shiro权限安全框架的使用"/></a><div class="content"><a class="title" href="/posts/78afc3f0ab42/" title="Shiro权限安全框架的使用">Shiro权限安全框架的使用</a><time datetime="2022-09-18T11:43:29.000Z" title="发表于 2022-09-18 19:43:29">2022-09-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/e32230f09e09/" title="学生成长动态追踪系统"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统"/></a><div class="content"><a class="title" href="/posts/e32230f09e09/" title="学生成长动态追踪系统">学生成长动态追踪系统</a><time datetime="2022-08-25T11:43:29.000Z" title="发表于 2022-08-25 19:43:29">2022-08-25</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86.assets/jasypt.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By zylai</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='/'|| '/' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>